<?php

namespace Swof\RPC;

use Swoole\Server;

class Rpc
{
    protected $host;

    protected $port;

    public function __construct(Server $server, array $config)
    {
        $this->host = $config['host'];
        $this->port = $config['port'];

        $port = $server->listen($this->host, $this->port, SWOOLE_SOCK_TCP);
        $port->set($config['swo']);

        $port->on('connect', [$this, 'connect']);
        $port->on('receive', [$this, 'receive']);
        $port->on('close', [$this, 'close']);
    }

    public function connect(Server $server, $fd)
    {
        echo '监听端口连接开始：' . PHP_EOL;
    }

    public function receive(Server $server, $fd, $from_id, $data)
    {
        $server->send($fd, "监听端口接收: " . $data);
        $server->close($fd);
    }

    public function close(Server $server, $fd)
    {
        echo '监听端口关闭' .  PHP_EOL;
    }
}